﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using GestorSQL.BLL;
using GestorSQL.BusinessEntities;

namespace GestorSQL.GUI
{
    public partial class ViewCase : Form
    {
        private CaseManager _caseManager = new CaseManager();
        private SolutionManager _solutionManager = new SolutionManager();
        private BLLDescompressReader _bllDescompressReader = new BLLDescompressReader();

        public ViewCase()
        {
            InitializeComponent();
            EnableFechaInicioFinChecked(false);
        }


        public void EnableFechaInicioFinChecked(bool check)
        {
            dtInicio.Enabled = false;
            dtFin.Enabled = false;
        }

        private void cbFechaInicio_CheckedChanged(object sender, EventArgs e)
        {
            if ((sender as CheckBox).Checked)
                dtInicio.Enabled = true;
            else
                dtInicio.Enabled = false;
        }

        private void cbFechafin_CheckedChanged(object sender, EventArgs e)
        {
            if ((sender as CheckBox).Checked)
                dtFin.Enabled = true;
            else
                dtFin.Enabled = false;
        }

        private void btnBuscar_Click(object sender, EventArgs e)
        {

            DateTime? fechaInicio = null;
            DateTime? fechaFin = null;

            if (cbFechaInicio.Checked)
                fechaInicio = dtInicio.Value;

            if (cbFechafin.Checked)
                fechaFin = dtFin.Value;

            gridCasos.DataSource = _caseManager.ViewCasesByParameters(tbNumeroCaso.Text, tbUsuarioAnalista.Text, tbUsuarioDueño.Text, fechaInicio, fechaFin,cbMostrarAsistidos.Checked);



        }

        private void ViewCase_Load(object sender, EventArgs e)
        {

        }

        private void gridCasos_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

            BLLDescompressReader bblDescompressReader = null;

            if (e.RowIndex != -1)
            {
                ConsultaCaso consulta = (gridCasos.Rows[e.RowIndex].DataBoundItem as ConsultaCaso);
                string scriptOriginal = string.Empty;
                string scriptFinalEnProduccion = string.Empty;
                TextWindow textWindow = new TextWindow("", "", "", true,true,false);
                string input;


                if (e.ColumnIndex == 0)
                {

                    textWindow = new TextWindow(Properties.Resources.Solution_QueryOriginalSolution, "", Properties.Resources.Query+":", true,true,false);
                    scriptOriginal = _solutionManager.GetOriginalSolution(consulta.NumeroCaso, consulta.IdSolucion);
                    textWindow.txt_Texto.Text = scriptOriginal;
                }
                else if (e.ColumnIndex == 1)
                {

                    textWindow = new TextWindow(Properties.Resources.Solution_QueryProductionlSolution, "", Properties.Resources.Query+":", true, true, false);
                    bblDescompressReader = _solutionManager.GetFinalScriptSolution(consulta.NumeroCaso, consulta.IdSolucion);

                    while (((input = bblDescompressReader.ReadLine()) != null))
                    {
                        textWindow.txt_Texto.AppendText(input);
                        textWindow.txt_Texto.AppendText("\n");

                    }

                    bblDescompressReader.Dispose();
                }

                if (e.ColumnIndex == 2)
                {
                    textWindow = new TextWindow(Properties.Resources.Solution_ResultProductionExecute, "", Properties.Resources.Result+":", true,false,false);
                    bblDescompressReader = _solutionManager.GetSalidaScriptSolution(consulta.NumeroCaso, consulta.IdSolucion);

                    while (((input = bblDescompressReader.ReadLine()) != null))
                    {
                        textWindow.txt_Texto.AppendText(input);
                        textWindow.txt_Texto.AppendText("\n");
                    }

                    bblDescompressReader.Dispose();

                }


                textWindow.ShowDialog();
            }
        }
    }
}
